﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Core.Security
{
    public sealed class CofePrincipal : IPrincipal
    {
        #region Constructor

        internal CofePrincipal(IPrincipalPolicy[] policies, IIdentity identity, bool isService = false)
        {
            Identity = identity;
            List<string> roleList = new List<string>();

            if (identity != null)
                foreach (var policy in policies)
                    if (policy.Match(identity))
                        roleList.AddRange(policy.GrantRole);

            if (isService)
                roleList.Add(SecurityConstants.ServiceRole);

            _grantedRole = roleList.ToArray();
        }

        #endregion

        #region Methods

        public bool IsInRole(string role)
        {
            return _grantedRole.Contains(role);
        }

        #endregion

        #region Data

        string[] _grantedRole;

        #endregion

        #region Public Properties

        public IIdentity Identity { get; private set; }

        #endregion

    }
}
